package com.example.javaow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.javaow.domain.entity.DeviceMessage;
import com.example.javaow.mapper.DeviceMessageMapper;
import com.example.javaow.service.DeviceMessageService;
import org.springframework.stereotype.Service;

/**
 * 设备消息服务实现类
 * 实现设备消息相关的业务逻辑
 */
@Service
public class DeviceMessageServiceImpl extends ServiceImpl<DeviceMessageMapper, DeviceMessage> implements DeviceMessageService {
    // 继承 ServiceImpl 已实现基本的增删改查功能
    // 可在此处实现自定义业务逻辑
    @Override
    public IPage<DeviceMessage> getPageWithConditions(Integer pageNum, Integer pageSize,
                                                      String messageType, String deviceAddress,
                                                      String deviceName, String status) {
        // 创建分页对象
        Page<DeviceMessage> page = new Page<>(pageNum, pageSize);

        // 构建查询条件
        QueryWrapper<DeviceMessage> queryWrapper = new QueryWrapper<>();

        // 消息类型查询
        if (messageType != null && !messageType.isEmpty()) {
            queryWrapper.eq("message_type", messageType);
        }

        // 设备地址查询
        if (deviceAddress != null && !deviceAddress.isEmpty()) {
            queryWrapper.eq("device_address", deviceAddress);
        }

        // 设备名称查询
        if (deviceName != null && !deviceName.isEmpty()) {
            queryWrapper.like("device_name", deviceName);
        }

        // 通讯状态查询
        if (status != null && !status.isEmpty()) {
            queryWrapper.eq("status", status);
        }

        // 按创建时间降序排列
        queryWrapper.orderByDesc("createtime");

        // 执行分页查询
        return this.page(page, queryWrapper);
    }

}
